package org.study.module.hive;

import org.study.entity.ClusterInfoEntity;
import org.study.entity.ClusterModuleInfoEntity;
import org.study.module.common.ModuleCommon;
import org.study.shell.ShellBase;

import java.util.ArrayList;
import java.util.List;

/**
 * Hive安装shell命令
 */
public class HiveShellBuilder {
    private List<String> commands;

    public HiveShellBuilder() {
        commands = new ArrayList<>();
    }

    public HiveShellBuilder existVersion() {
        commands.add("jdk_v=`java -version`");
        commands.add("if [[ $jdk_v == *\"jdk\"* ]]; then");
        commands.add("    echo \"JDK已安装！版本$jdk_v！\"");
        commands.add("    exit 1");
        commands.add("fi");
        return this;
    }


    public HiveShellBuilder check() {
        commands.add("java -version");
        return this;
    }

    public HiveShellBuilder install(ClusterInfoEntity infoEntity, ClusterModuleInfoEntity moduleMode) {
        commands.addAll(ModuleCommon.installByTar(infoEntity, moduleMode));
        return this;
    }

    public HiveShellBuilder source(){
        List<String> pros = new ArrayList<>();
        pros.add("export HIVE_HOME=$hive_install");
        pros.add("export PATH=\\$PATH:\\$HIVE_HOME/bin");
        commands.addAll(ShellBase.addProfile(pros, "hive_env.sh"));
        return this;
    }

    public HiveShellBuilder metastore(){
        commands.add("nohup hive --service metastore");
        return this;
    }
    public HiveShellBuilder hiveServer2(){
        commands.add("nohup hive --service hiveserver2");
        return this;
    }

    public List<String> toList() {
        return commands;
    }
}
